package com.qf.dao;

import com.qf.pojo.Profile;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author fy
 * @version V1.0
 * @Project day41
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/27 13:52
 */
public class ProfileDaoImpl implements ProfileDao {
    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Profile> findAll() {
        String sql = "select * from profile";
        try {
            return queryRunner.query(sql, new BeanListHandler<Profile>(Profile.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Profile findById(Integer id) {
        String sql = "select * from profile where id=?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(Profile.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public void updateByUserId(Integer id,Profile profile) {
        String sql = "update profile set name=?,birthday=?,gender=?,career=?,address=?,mobile=?,pricture=?";
        try {
            queryRunner.update(sql, profile.getName(),profile.getBirthday(),profile.getGender(),profile.getCareer(),profile.getAddress(),profile.getMobile(),profile.getPicture());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
